﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Servers
{
    public class DiaryServers
    {
        private readonly IRepository<diary> diaryRepository;
        private readonly IRepository<diary_comment> diaryCommentRepository;
        private readonly IRepository<diary_comment_reply> diaryCommentReplyRepository;
        private readonly IRepository<user> usersRepository;

        public DiaryServers()
        {
            diaryRepository = new DapperRepository<diary>();
            diaryCommentRepository = new DapperRepository<diary_comment>();
            diaryCommentReplyRepository = new DapperRepository<diary_comment_reply>();
            usersRepository = new DapperRepository<user>();
        }

        public List<diary> GetData()
        {
            var datas = diaryRepository.GetAllEnumerable();

            return datas.ToList();
        }

        public List<diary> GetData(int userId, int status = 0)
        {
            var datas = diaryRepository.GetAllEnumerable();
            switch (status)
            {
                case 1:
                    datas = datas.Where(a => a.USER_ID == userId);
                    break;
                case 2:
                    datas = datas.Where(a => a.USER_ID != userId && a.TO_ALL);
                    break;
                default:
                    datas = datas.Where(a => a.USER_ID == userId || a.TO_ALL);
                    break;
            }
            return datas.OrderByDescending(a => a.DIA_TIME).ToList();
        }

        public IList<diary> GetDiarySearchData(int type, int id, string querydate)
        {
            string sql;
            if (type == 1)
            {
                sql = "SELECT D.* FROM [OADB].[dbo].[diary] AS D INNER JOIN [OADB].[dbo].[users] AS U ON D.[USER_ID] = U.[USER_ID] WHERE D.[TO_ALL]=1 AND U.[DEPT_ID] = " + id;
            }
            else
            {
                sql = "SELECT * FROM [OADB].[dbo].[diary] WHERE [TO_ALL]=1 AND [USER_ID] = " + id;
            }

            if (!string.IsNullOrEmpty(querydate))
            {
                sql += " AND [DIA_TIME] >= '" + querydate + "' AND [DIA_TIME] < DATEADD(dd,1,'" + querydate + "')";
            }
            var tables = diaryCommentRepository.SqlWithParamsDynamic(sql);
            IList<diary> datalist = new List<diary>();
            foreach (var item in tables)
            {
                datalist.Add(new diary
                {
                    USER_ID = item.USER_ID,
                    DIA_DATE = item.DIA_DATE,
                    DIA_TIME = item.DIA_TIME,
                    DIA_TYPE = item.DIA_TYPE,
                    SUBJECT = item.SUBJECT,
                    CONTENT = item.CONTENT,
                    ATTACHMENT_ID = item.ATTACHMENT_ID,
                    ATTACHMENT_NAME = item.ATTACHMENT_NAME,
                    LAST_COMMENT_TIME = item.LAST_COMMENT_TIME,
                    TO_ID = item.TO_ID,
                    //COMPRESS_CONTENT = item.COMPRESS_CONTENT,
                    READERS = item.READERS,
                    TO_ALL = Convert.ToBoolean(item.TO_ALL),
                    DIA_ID = item.DIA_ID
                });
            }
            return datalist;
        }

        public diary GetModelById(int id)
        {
            return diaryRepository.GetById(id);
        }

        public user GetUsers(int userId)
        {
            string sql = "SELECT * FROM [OADB].[dbo].[users] where [USER_ID]=" + userId;
            return usersRepository.SqlWithParamsSingle(sql);
        }

        public user GetUsers(string userName)
        {
            string sql = string.Format("SELECT * FROM [OADB].[dbo].[users] where [USER_NO]='{0}'", userName);
            return usersRepository.SqlWithParamsSingle(sql);
        }

        public IList<diary_comment> GetDiaryComment(int diaryId)
        {
            string sql = "SELECT [DIA_ID],[USER_ID],[CONTENT],[SEND_TIME],[COMMENT_FLAG],[ATTACHMENT_ID],[ATTACHMENT_NAME],[COMMENT_ID] FROM [OADB].[dbo].[diary_comment] WHERE [DIA_ID]=" + diaryId;
            var tables = diaryCommentRepository.SqlWithParamsDynamic(sql);
            IList<diary_comment> datalist = new List<diary_comment>();
            foreach (var item in tables)
            {
                datalist.Add(new diary_comment
                {
                    DIA_ID = item.DIA_ID,
                    USER_ID = item.USER_ID,
                    CONTENT = item.CONTENT,
                    SEND_TIME = item.SEND_TIME,
                    COMMENT_FLAG = item.COMMENT_FLAG,
                    ATTACHMENT_ID = item.ATTACHMENT_ID,
                    ATTACHMENT_NAME = item.ATTACHMENT_NAME,
                    COMMENT_ID = item.COMMENT_ID
                });
            }
            return datalist;
        }

        public IList<diary_comment_reply> GetDiaryCommentReplay(int commentId)
        {
            string sql = "SELECT [REPLY_COMMENT],[REPLY_TIME],[REPLYER],[COMMENT_ID],[TO_ID],[REPLY_ID] FROM [OADB].[dbo].[diary_comment_reply] WHERE [COMMENT_ID]=" + commentId;
            var tables = diaryCommentReplyRepository.SqlWithParamsDynamic(sql);
            IList<diary_comment_reply> datalist = new List<diary_comment_reply>();
            foreach (var item in tables)
            {
                datalist.Add(new diary_comment_reply
                {
                    REPLY_COMMENT = item.REPLY_COMMENT,
                    REPLY_TIME = item.REPLY_TIME,
                    REPLYER = item.REPLYER,
                    COMMENT_ID = item.COMMENT_ID,
                    TO_ID = item.TO_ID,
                    REPLY_ID = item.REPLY_ID
                });
            }
            return datalist;
        }

        internal void Add(diary model)
        {
            diaryRepository.Insert(model);
        }

        internal void AddComment(diary_comment model)
        {
            diaryCommentRepository.Insert(model);
        }

        internal void AddCommentReply(diary_comment_reply model)
        {
            diaryCommentReplyRepository.Insert(model);
        }

        internal void Update(diary model)
        {
            diaryRepository.Update(model);
        }

        internal void Del(string id)
        {
            diaryRepository.DeleteByIds(id);
        }

        internal void SetSharedStatus(int id, short isShared)
        {
            string sql = "UPDATE [dbo].[diary] SET [TO_ALL] = " + isShared + " WHERE DIA_ID = " + id;
            diaryRepository.SqlWithParams(sql);
        }

        internal void UpdateReaders(int id)
        {
            string sql = "UPDATE [dbo].[diary] SET [READERS] = [READERS]+1 WHERE DIA_ID = " + id;
            diaryRepository.SqlWithParams(sql);
        }

        internal List<SelectListItem> GetDIA_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = diaryRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 177");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


    }
}